package cn.ptu.pmaa.service;

import cn.ptu.pmaa.entities.*;
import cn.ptu.pmaa.mapper.AdminMapper;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.swing.text.TabExpander;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminService {

    @Autowired(required = false)
    AdminMapper adminMapper;

    //查询所有学生账号信息
    public List<Student> getAllStudentInfo(){
        return adminMapper.getAllStudentInfo();
    }
    //根据id查询学生账号信息
    public List<Student> getStudentInfoById(String id){
        return adminMapper.getStudentInfoById(id);
    }
    //根据姓名查询学生账号信息
    public List<Student> getStudentInfoByName(String name){
        return adminMapper.getStudentInfoByName(name);
    }
    //修改学生账号信息
    public int editStudentInfoById(Student student){
        return adminMapper.editStudentInfoById(student);
    }
    //添加学生账号信息
    public int addStudent(Student student) throws Exception{
        return adminMapper.addStudent(student);
    }
    //批量添加学生账号信息
    public int addAllStudent(String excelAbsoluteRoute){
        // 定义一个数据格式化对象
        XSSFWorkbook wb = null;
        //获取sheet表格，及读取单元格内容
        XSSFSheet sheet = null;
        try {
            //excel模板路径
            String file = excelAbsoluteRoute;

            File cfgFile = ResourceUtils.getFile(file);
            InputStream in = new FileInputStream(cfgFile);
            //读取excel模板
            wb = new XSSFWorkbook(in);
            sheet = wb.getSheetAt(0);

            List<Student> list = new ArrayList<>();
            String[] ss = new String[3];
            ss[0]="student";
//            Student student = new Student();
//            student.setRole("student");
            for (int i=1;i<=sheet.getLastRowNum();i++){
                Student student = new Student();
                student.setRoles("student");
                sheet.getRow(i).getCell(1).setCellType(CellType.STRING);
                sheet.getRow(i).getCell(0).setCellType(CellType.STRING);
                String cellValue0 = sheet.getRow(i).getCell(0).getStringCellValue();
                String cellValue1 = sheet.getRow(i).getCell(1).getStringCellValue();
                student.setStudentId(cellValue0);student.setStudentPwd(cellValue1);
                list.add(student);
            }
//            for (Student s:list
//                 ) {
//                System.out.println(s);
//            }
            return adminMapper.addAllStudent(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    //删除学生账号信息
    public int deleteStudentById(String id){
        return adminMapper.deleteStudentById(id);
    }
    //批量删除学生账号信息
    public int deleteAllStudentById(String[] ids){
        return adminMapper.deleteAllStudentById(ids);
    }


    //查询所有教师账号信息
    public List<Teacher> getAllTeacherInfo(){
        return adminMapper.getAllTeacherInfo();
    }
    //根据id查询教师账号信息
    public List<Teacher> getTeacherInfoById(String id){
        return adminMapper.getTeacherInfoById(id);
    }
    //根据姓名查询教师账号信息
    public List<Teacher> getTeacherInfoByName(String name){
        return adminMapper.getTeacherInfoByName(name);
    }
    //修改教师账号信息
    public int editTeacherInfoById(Teacher teacher){
        return adminMapper.editTeacherInfoById(teacher);
    }
    //添加教师账号信息
    public int addTeacher(Teacher teacher){
        return adminMapper.addTeacher(teacher);
    }
    //删除教师账号信息
    public int deleteTeacherById(String id){
        return adminMapper.deleteTeacherById(id);
    }

    //动态查询预约信息：日期，预约人角色，预约人id，开始时间，结束时间，教室，状态
    public List<Appintment> getAppintment(Appintment appintment){
        return adminMapper.getAppintment(appintment);
    }
    //修改预约信息
    public int editAppintmentById(Appintment appintment){
        return adminMapper.editAppintmentById(appintment);
    }
    //删除预约信息
    public int deleteAppintmentById(String id){
        return adminMapper.deleteAppintmentById(id);
    }
    //批量删除预约信息
    public int deleteAllAppintmentById(String[] ids){
        return adminMapper.deleteAllAppintmentById(ids);
    }

    //管理教室信息
    //查询所有教室信息
    public List<ClassRoom> getAllClassRoomInfo(){
        return adminMapper.getAllClassRoomInfo();
    }
    //修改教室信息
    public int editClassRoomById(ClassRoom classRoom){
        return adminMapper.editClassRoomById(classRoom);
    }
    //添加教室信息
    public int addClassRoom(ClassRoom classRoom){
        return adminMapper.addClassRoom(classRoom);
    }
    ////删除教室账号信息
    public int deleteClassRoomById(String id){
        return adminMapper.deleteClassRoomById(id);
    }
    //按条件查询教室信息：jsid，jsld，jsfh，jszws，sfqy，sfkfgxs
    public List<ClassRoom> getClassRoomInfo(ClassRoom classRoom){
        return adminMapper.getClassRoomInfo(classRoom);
    }

    //查看反馈信息
    public List<FeedBack> getFeedBack(){
        return adminMapper.getFeedBack();
    }
    //删除反馈信息
    public int deleteFeedBack(String id){
        return adminMapper.deleteFeedBack(id);
    }

}
